<template>
  <el-dialog class="form" title="设置" :visible.sync="dialogFormVisible" :close-on-click-modal="false" :close-on-press-escape="false" :show-close="false">
    <el-form :model="form" label-width="100px" label-position="left" ref="form">
      <el-form-item label="根路径"
                    prop="rootPath"
                    :rules="[
                      { required: true, message: '不能为空'},
                    ]">
        <div class="df-center">
          <el-input type="rootPath" v-model="form.rootPath" auto-complete="off" class="df1"></el-input>
          <el-button type="primary" size="small" plain style="margin-left: 10px" @click.native="openDialog">一键选择</el-button>
        </div>
      </el-form-item>
      <el-form-item label="每页数量"
                    prop="pageSize"
                    :rules="[{ validator: checkAge, trigger: 'blur' }]">
        <el-input type="pageSize" v-model.number="form.pageSize" auto-complete="off"></el-input>
      </el-form-item>
      <el-form-item label="文件移动">
        <el-radio v-model="form.moveWay" :label="1">询问移动</el-radio>
        <el-radio v-model="form.moveWay" :label="2">复制放入</el-radio>
        <el-radio v-model="form.moveWay" :label="3">移动放入</el-radio>
        <div class="c999" style="font-size: 13px">tip: {{tip}}</div>
      </el-form-item>
      <el-form-item label="暂存区显示">
        <el-switch v-model="form.showTempArea"></el-switch>
      </el-form-item>
      <el-form-item label="开机自启动">
        <el-switch v-model="form.autoRun"></el-switch>
      </el-form-item>
      <el-form-item>
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="save">保 存</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>

</template>

<style scoped lang="scss" rel="stylesheet/scss">
  .app-container-scroll {
    padding: 30px;
    box-sizing: border-box;
  }
  form {
    max-width: 500px;
  }
</style>

<script type="text/babel">
  import {mapGetters, mapActions} from 'vuex'
  // import dragMixin from '../components/dragMixin.js'
  import startOnBoot from '../lib/startOnBoot.js'

  export default {
    // mixins: [dragMixin],
    // beforeRouteEnter (from, to, next) {
    //   next(vm => {
    //     vm.init()
    //   })
    // },
    components: {},
    data() {
      return {
        form: {},
        dialogFormVisible: false
      }
    },
    watch: {},
    computed: {
      ...mapGetters(['setting']),
      tip () {
        if (this.form.moveWay == 1) {
          return '将询问你是【复制文件】还是【移动文件】来放入文件夹'
        }
        if (this.form.moveWay == 2) {
          return '复制文件放入文件夹，不影响源文件'
        }
        if (this.form.moveWay == 3) {
          return '移动文件放入文件夹，影响源文件'
        }
      }
    },
    methods: {
      ...mapActions(['updateAppSetting']),
      openForm () {
        this.dialogFormVisible = true
        this.init()
      },
      checkAge (rule, value, callback) {
        if (!value) {
          return callback(new Error('不能为空'));
        }
        setTimeout(() => {
          if (!Number.isInteger(value)) {
            callback(new Error('请输入数字值'));
          } else {
            if (value < 5) {
              callback(new Error('最少5个每页'));
            } else {
              callback();
            }
          }
        }, 800);
      },
      init () {
        this.form = JSON.parse(JSON.stringify(this.setting))
      },
      cancel () {
        // this.$router.back()
        if (!this.setting.rootPath) {
          return
        }
        this.dialogFormVisible = false
      },
      save () {
        this.$refs['form'].validate((valid) => {
          if (valid) {
            let rootPath = this.form.rootPath.trim()
            rootPath = ['/', '\\'].indexOf(rootPath.substr(rootPath.length - 1, 1)) < 0 ? rootPath + '\\' : rootPath
            this.form.rootPath = rootPath

            this.$db.set('setting', this.form).write()
            this.updateAppSetting({
              setting: JSON.parse(JSON.stringify(this.form))
            })
            this.setAutoRun()
            this.$message({
              message: '保存成功！',
              type: 'success',
              duration: 1000,
            });

            // this.$router.replace({
            //   name: 'index'
            // })
            this.dialogFormVisible = false

          } else {
            console.log('error submit!!');
            return false;
          }
        })

      },
      openDialog () {
        this.$electron.ipcRenderer.send('open-file-dialog')
      },
      dialogSelectedFile (event, files) {
        this.form.rootPath = files[0]
      },
      //  配置开机自启动
      setAutoRun () {
        let appName     = process.env.NODE_ENV == 'development' ? 'LilyGo_dev' : 'LilyGo'
        if (this.setting.autoRun) {
          startOnBoot.enableAutoStart(appName, process.execPath);
        } else {
          startOnBoot.disableAutoStart(appName)
        }
      }
    },
    mounted() {
      this.$electron.ipcRenderer.on('dialog-selected-file', this.dialogSelectedFile);
    },
  }
</script>

